//
//  NewBookmarkViewController.m
//  eBook
//
//  Created by Dimaginexus on 14-5-23.
//  Copyright (c) 2014年 zhan. All rights reserved.
//

#import "NewBookmarkViewController.h"

@interface NewBookmarkViewController ()

@end

@implementation NewBookmarkViewController

@synthesize NewBookmarkDelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (!XAppDelegate.bookmarkData)
    {
        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:[UIImage imageNamed:@"cover.png"], @"image",
                              @"问卷调查法：问卷组成", @"content", @"1", @"feature", nil];
        NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:[UIImage imageNamed:@"cover.png"], @"image",
                              @"问卷调查法：制定问卷", @"content", @"2", @"feature", nil];
        
        XAppDelegate.bookmarkData = [NSMutableArray arrayWithObjects:dic1, dic2, nil];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - tableView delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [XAppDelegate.stackVC popViewControllerAnimated:YES];
    
    NSDictionary *dic = [XAppDelegate.bookmarkData objectAtIndex:indexPath.row];
    
    NSString *chapterTemp = [dic objectForKey:@"feature"];
    
    [NewBookmarkDelegate loadBookmarkChapter:chapterTemp.intValue];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return XAppDelegate.bookmarkData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NewBookmarkCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
    UILabel *contentLabel = (UILabel *)[cell viewWithTag:101];
    
    NSDictionary *dic = [XAppDelegate.bookmarkData objectAtIndex:indexPath.row];
    
    imageView.image = [dic objectForKey:@"image"];
    contentLabel.text = [dic objectForKey:@"content"];
    
    return cell;
}

-(BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

#pragma Actions

- (IBAction)dismissVC:(id)sender
{
    [XAppDelegate.stackVC popViewControllerAnimated:YES];
    [NewBookmarkDelegate newBookmarkVCBackToMenu];
}

- (IBAction)addBookmark:(id)sender
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.image, @"image",
                          self.content, @"content", self.feature, @"feature", nil];
    
    [XAppDelegate.bookmarkData addObject:dic];
    
    [self.tableView reloadData];
}
@end
